import 'package:casdon/app/config/theme/theme_extention.dart';
import 'package:casdon/build_gen/assets.gen.dart';
import 'package:casdon/modules/lifeCircle/ui/widgets/status_button_widget.dart';
import 'package:casdon/modules/me/ui/widgets/circle_avatar_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class AuthorHeaderWidget extends StatelessWidget {
  final String avatarUrl;
  final String nickname;
  final bool isOfficial;
  final String createTime;
  final bool? isFollowed;
  final bool? showFollowBtn;
  final VoidCallback? onFollowTap;
  final VoidCallback? onAvaterTap;

  const AuthorHeaderWidget({
    super.key,
    required this.avatarUrl,
    required this.nickname,
    required this.createTime,
    required this.isOfficial,
    required this.showFollowBtn,
    this.isFollowed,
    this.onFollowTap,
    this.onAvaterTap,
  });

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        // 左侧区域（头像 + 用户名 + 时间）- 整体可点击
        Expanded(
          child: GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () => _handleAvatarClick(context),
            child: Container(
              constraints: BoxConstraints(minHeight: 44.w),
              padding: EdgeInsets.only(
                right: showFollowBtn == true ? 10.w : 0,
              ),
              child: Row(
                children: [
                  // Avatar
                  CircleAvatarButton(
                    imageUrl: avatarUrl,
                    defaultImage: isOfficial
                        ? AssetImage(Assets.images.icOfficeAvatar.path)
                        : AssetImage(Assets.images.icAvatar.path),
                    size: 36.w,
                    onPressed: () => _handleAvatarClick(context),
                  ),

                  SizedBox(width: 10.w),

                  // 用户名 + 官方标识 + 时间
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Flexible(
                              child: Text(
                                nickname,
                                style: context.textTheme.bodyMedium?.copyWith(
                                  color: context
                                      .customColorThemeNonNull.textColor1,
                                ),
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                            if (isOfficial) ...[
                              SizedBox(width: 6.w),
                              Container(
                                padding: EdgeInsets.symmetric(
                                  horizontal: 5.w,
                                  vertical: 2.w,
                                ),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(6.w),
                                  gradient: const LinearGradient(
                                    begin: Alignment.centerLeft,
                                    end: Alignment.centerRight,
                                    colors: [
                                      Color(0xFFFFAF00),
                                      Color(0xFFFA9550),
                                    ],
                                  ),
                                ),
                                child: Text(
                                  '官方',
                                  style: TextStyle(
                                    fontSize: 11.r,
                                    fontWeight: FontWeight.w400,
                                    color: Colors.white,
                                  ),
                                ),
                              ),
                            ],
                          ],
                        ),
                        SizedBox(height: 2.w),
                        Text(
                          createTime,
                          style: context.textTheme.bodySmall?.copyWith(
                            color: context.customColorThemeNonNull.textColor2,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),

        // 关注按钮
        if (showFollowBtn == true)
          StatusButton(
            isActive: isFollowed ?? false,
            activeText: '关注的人',
            inactiveText: '关注',
            activeTextStyle: context.textTheme.bodyMedium?.copyWith(
              color: context.customColorThemeNonNull.textColor3,
            ),
            inactiveTextStyle: context.textTheme.bodyLarge?.copyWith(
              color: context.colorScheme.primary,
            ),
            activeColor: Colors.white,
            inactiveColor: Colors.white,
            activeBorderColor: Colors.white,
            inactiveBorderColor: context.colorScheme.primary,
            onTap: () {
              if ((isFollowed ?? false) == false) onFollowTap?.call();
            },
          ),
      ],
    );
  }

  void _handleAvatarClick(BuildContext context) {
    onAvaterTap?.call();
  }
}
